/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Form;
import com.sun.lwuit.Command;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import core.Settings;

/**
 *
 * @author Kvest
 */
public class SettingsForm extends Form implements ActionListener
{
    public static final int SAVE_ID = 1;
    public static final int BACK_ID = 2;

    private Command save;
    private Command back;
    private FormAction listener;

    private TextField nick;
    private TextField login;
    private TextField password;
    private CheckBox rem;
    private TextField urlTF;
    private CheckBox updateInterface;
    private CheckBox fullOrder;
    private CheckBox takeOrder;
    private CheckBox showPhone;
    private CheckBox showAllMassages;
    private TextField dispetcherCall;
    private TextField dispetcherSMS;
    private TextField freeWaitTime;

    public SettingsForm(FormAction listener)
    {
        super("Позывной");
        this.listener = listener;

        init();
    }

    public void show()
    {
        Settings settings = Settings.getSettings();
        
        nick.setText(settings.getNick());
        login.setText(settings.getLogin());
        password.setText(settings.getPassword());
        rem.setSelected(settings.isRememberPassword());
        urlTF.setText(settings.getServerIP());
        updateInterface.setSelected(settings.isUpdateInterface());
        fullOrder.setSelected(settings.isFullOrder());
        takeOrder.setSelected(settings.isTakeOrder());
        showPhone.setSelected(settings.isShowPhone());
        showAllMassages.setSelected(settings.isShowAllMassages());
        dispetcherCall.setText(settings.getDispetcherCall());
        dispetcherSMS.setText(settings.getDispetcherSMS());
        if (ViewManager.getViewManager().getFunctionMenuType() == FunctionMenu.CHERNOVTSI_TYPE)
        {
            freeWaitTime.setText(Integer.toString(settings.getFreeWaitTime()));
        }
        
        super.show();
    }

    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        
        addCommandListener(this);

        back= new Command("Назад", BACK_ID);
        addCommand(back);

        save = new Command("Сохранить", SAVE_ID);
        addCommand(save);

        Settings settings = Settings.getSettings();
        int functionMenuType = ViewManager.getViewManager().getFunctionMenuType();
        
        //Создаем компоненты на форме
        //Позывной
        addComponent(new Label("Позывной:"));
        nick = new TextField();
        nick.setSelectCommandText("");
        nick.setConstraint(TextArea.ANY);
        addComponent(nick);
        
        //Логин
        addComponent(new Label("Логин:"));
        login = new TextField();
        login.setSelectCommandText("");
        login.setConstraint(TextArea.ANY);
        addComponent(login);

        //Пароль
        addComponent(new Label("Пароль:"));
        password = new TextField();
        password.setSelectCommandText("");
        password.setConstraint(TextArea.ANY);
        addComponent(password);

        //Флаг сохранения пароля
        rem = new CheckBox("Сохранить пароль");
        rem.setSelectCommandText("");
        addComponent(rem);

        //Сервер ip
        addComponent(new Label("Сервер:"));
        urlTF = new TextField();
        urlTF.setSelectCommandText("");
        urlTF.setConstraint(TextArea.ANY);
        addComponent(urlTF);

        //Автообновление <интерфейса>
        updateInterface = new CheckBox("Автообновление интерфейса");
        updateInterface.setSelectCommandText(""); 
        addComponent(updateInterface);

        //Все о заказе
        fullOrder = new CheckBox("Все о заказе");
        fullOrder.setSelectCommandText("");
        addComponent(fullOrder);

        //"Брать заказы"
        takeOrder = new CheckBox("Брать заказ");
        takeOrder.setSelectCommandText("");
        addComponent(takeOrder);

        //Показывать телефон клиента
        showPhone = new CheckBox("Показывать телефон клиента");
        showPhone.setSelectCommandText("");
        addComponent(showPhone);
        
        //Показывать ли все сообщения
        showAllMassages = new CheckBox("Все сообщения");
        showAllMassages.setSelectCommandText("");
        addComponent(showAllMassages);

        //Телефон диспетчера для звонков  
        dispetcherCall = new TextField();
        dispetcherCall.setSelectCommandText("");
        dispetcherCall.setConstraint(TextArea.ANY);
        dispetcherCall.setInputMode("123");
        if (functionMenuType != FunctionMenu.KIROVOGRAD_TYPE)
        { 
            addComponent(new Label("Диспетчер(звонок):"));
            addComponent(dispetcherCall);
        }

        //Телефон диспетчера для смс
        addComponent(new Label("Диспетчер(СМС):"));
        dispetcherSMS = new TextField();
        dispetcherSMS.setSelectCommandText("");
        dispetcherSMS.setConstraint(TextArea.ANY);
        dispetcherSMS.setInputMode("123");
        addComponent(dispetcherSMS);

        //Бесплатное ожидание
        if (functionMenuType == FunctionMenu.CHERNOVTSI_TYPE)
        {
            addComponent(new Label("Бесплатное ожидание, мин:"));
            freeWaitTime = new TextField();
            freeWaitTime.setSelectCommandText("");
            freeWaitTime.setConstraint(TextArea.NUMERIC);
            freeWaitTime.setInputModeOrder(new String[]{"123"});
            addComponent(freeWaitTime);
            addComponent(new Label(" "));
        }
    }

    public void actionPerformed(ActionEvent evt)
    {
        Command cmd = evt.getCommand();
        switch (cmd.getId())
        {
            case SAVE_ID : saveAction(); break;
        }

        if (listener != null)
        {
            listener.commandAction(cmd.getId());
        }
    }

    private void saveAction()
    {
        Settings settings = Settings.getSettings();

        settings.setNick(nick.getText());
        settings.setLogin(login.getText());
        settings.setPassword(password.getText());
        settings.setRememberPassword(rem.isSelected());
        settings.setServerIP(urlTF.getText());
        settings.setUpdateInterface(updateInterface.isSelected());
        settings.setFullOrder(fullOrder.isSelected());
        settings.setTakeOrder(takeOrder.isSelected());
        settings.setShowPhone(showPhone.isSelected());
        settings.setShowAllMassages(showAllMassages.isSelected());
        settings.setDispetcherCall(dispetcherCall.getText());
        settings.setDispetcherSMS(dispetcherSMS.getText());
        if (ViewManager.getViewManager().getFunctionMenuType() == FunctionMenu.CHERNOVTSI_TYPE)
        {
            try
            {
                settings.setFreeWaitTime(Integer.parseInt(freeWaitTime.getText()));
            }catch(Exception ex){}
        }
    }
}
